/*
 *  Copyright (c) 2022 ZhuHai Jieli Technology Co.,Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifndef CIRCULAR_BUF_INTERFACE_H
#define CIRCULAR_BUF_INTERFACE_H

#include "generic/typedef.h"

typedef struct _cbuffer {
    u8 *begin;
    u8 *end;
    u8  *read_ptr;
    u8  *write_ptr;
    u8  *tmp_ptr ;
    u32 tmp_len;
    u32 data_len;
    u32 total_len;
} cbuffer_t;

extern void cbuf_init(cbuffer_t *cbuffer, void *buf, u32 size);

extern u32 cbuf_read(cbuffer_t *cbuffer, void *buf, u32 len);

extern u32 cbuf_write(cbuffer_t *cbuffer, void *buf, u32 len);

extern u32 cbuf_is_write_able(cbuffer_t *cbuffer, u32 len);

extern void *cbuf_write_alloc(cbuffer_t *cbuffer, u32 *len);

extern void  cbuf_write_updata(cbuffer_t *cbuffer, u32 len);

void *cbuf_read_alloc(cbuffer_t *cbuffer, u32 *len);

void cbuf_read_updata(cbuffer_t *cbuffer, u32 len);

void cbuf_clear(cbuffer_t *cbuffer);

u32 cbuf_rewrite(cbuffer_t *cbuffer, void *begin, void *buf, u32 len);

void  cbuf_discard_prewrite(cbuffer_t *cbuffer);

void cbuf_updata_prewrite(cbuffer_t *cbuffer);

u32 cbuf_prewrite(cbuffer_t *cbuffer, void *buf, u32 len);

#define cbuf_get_writeptr(a) (a)->write_ptr

#define cbuf_get_data_size(a) (a)->data_len

#define cbuf_get_readptr(a ) (a)->read_ptr

#endif

